package com.yc.jpyy.meetcheck.service.imp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.yc.jpyy.base.service.BaseService;
import com.yc.jpyy.meetcheck.dao.MeetCheckDao;
import com.yc.jpyy.meetcheck.service.MeetCheckService;

@Service
public class MeetCheckServiceImpl extends BaseService implements MeetCheckService {
 

@Autowired
private MeetCheckDao meetCheckDao;

@Override
public Map<String, Object> addMeetCheck(Map<String, Object> paramsMap)
		throws DataAccessException {
	Map<String,Object> resultMap = new HashMap<>();
	//根据公司名和月份时间查询是否有重复数据
	int isRepeat = meetCheckDao.checkMeetRepeat(paramsMap);
	if(isRepeat <= 0){
		int count = meetCheckDao.addMeetCheck(paramsMap);
		if(count > 0){
			int k = meetCheckDao.addMeetCheckDetail(paramsMap);
			if(k < 0){
				resultMap.put("returnCode", 0);
				resultMap.put("returnMsg", "新增子表失败");
			}else{
				resultMap.put("returnCode", 1);
				resultMap.put("returnMsg", "新增成功");
			}
			/*List<Map<String,Object>> vehicleList = meetCheckDao.selectVehicles(paramsMap);
			if(vehicleList != null && vehicleList.size() > 0){
				for(int i = 0; i < vehicleList.size();i ++){
					vehicleList.get(i).put("dbcode", paramsMap.get("dbcode"));
					vehicleList.get(i).put("createman", paramsMap.get("createman"));
					vehicleList.get(i).put("meetingcheckid", paramsMap.get("ID"));
					vehicleList.get(i).put("companyid", paramsMap.get("companyid"));
					vehicleList.get(i).put("issubmitonblanklist", paramsMap.get("issubmitonblanklist"));
				}
				int cycle = vehicleList.size() % 50 == 0?vehicleList.size() / 50:vehicleList.size() / 50 + 1;
				for(int j = 0;j < cycle;j ++){
					int k = 0;
					if(j + 1 == cycle){
						k = meetCheckDao.addMeetCheckDetail(vehicleList.subList(j*50,vehicleList.size()));
					}else{
						k = meetCheckDao.addMeetCheckDetail(vehicleList.subList(j*50,(j+1)*50));
					}
					if(k < 0){
						try {
							throw new Exception();
						} catch (Exception e) {
							System.out.print("addMeetCheck" + e.toString());
						}
					}
				}
				resultMap.put("returnCode", 1);
				resultMap.put("returnMsg", "新增成功");
			}else{
				resultMap.put("returnCode", 1);
				resultMap.put("returnMsg", "新增成功");
			}*/
		}else{
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "新增主表失败");
		}
	}else{
		resultMap.put("returnCode", 0);
		resultMap.put("returnMsg", "该公司该月份已有会议记录");
	}
	return resultMap;
}


@Override
public Map<String, Object> selectMeetCheckById(Map<String, Object> paramsMap) throws DataAccessException {
	Map<String,Object> resultMap = new HashMap<>();
	Map<String,Object> meetInfo = meetCheckDao.selectMeetCheckById(paramsMap);
	if(meetInfo != null){
		resultMap.put("returnCode", 1);
		resultMap.put("ListData", meetInfo);
		resultMap.put("returnMsg", "操作成功");
	}else{
		resultMap.put("returnCode", 0);
		resultMap.put("returnMsg", "系统查询不到该条信息");
	}
	return resultMap;
}



@Override
public Map<String, Object> selectMeetCheckByPage(Map<String, Object> paramsMap)
		throws DataAccessException {
	Map<String,Object> resultMap = new HashMap<>();
	int pagenum = Integer.parseInt(paramsMap.get("pagenum").toString());
	int pagesize = Integer.parseInt(paramsMap.get("pagesize").toString());
	paramsMap.put("pagestart", (pagenum-1) * pagesize);
	String companyid = Optional.ofNullable(paramsMap.get("companyid")).orElse("").toString();
	paramsMap.put("companyid", companyid.equals("")?"":companyid.split(","));
	List<Map<String,Object>> meetCheckList = meetCheckDao.selectMeetCheckByPage(paramsMap);
	int meetCheckCounts = meetCheckDao.selectMeetCheckCount(paramsMap);
	if(meetCheckList != null && meetCheckList.size() > 0){
		resultMap.put("returnCode", 1);
		resultMap.put("ListData",meetCheckList);
		resultMap.put("TotalCount",meetCheckCounts);
		resultMap.put("TotalPage",meetCheckCounts % pagesize >0 ? meetCheckCounts/pagesize + 1 : meetCheckCounts/pagesize);
		resultMap.put("returnMsg", "操作成功");
	}else{
		resultMap.put("returnCode", 0);
		resultMap.put("returnMsg", "系统无数据");
	}
	return resultMap;
}

@Override
public Map<String, Object> delMeetCheck(Map<String, Object> paramsMap)
		throws DataAccessException {
	Map<String,Object> resultMap = new HashMap<>();
	paramsMap.put("ids", paramsMap.get("ids").toString().split(","));
	int count = meetCheckDao.delMeetCheck(paramsMap);
	if(count < 0){
		resultMap.put("returnCode", 0);
		resultMap.put("returnMsg", "删除失败");
	}else{
		int k = meetCheckDao.delMeetCheckDetail(paramsMap);
		if(k < 0){
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "删除失败");
		}else{
			resultMap.put("returnCode", 1);
			resultMap.put("returnMsg", "删除成功");
		}
	}
	return resultMap;
}


@Override
public Map<String, Object> selectMeetCheckAll(Map<String, Object> paramsMap)
		throws DataAccessException {
	Map<String,Object> resultMap = new HashMap<>();
	List<Map<String,Object>> meetCheckList = meetCheckDao.selectMeetCheckAll(paramsMap);
	if(meetCheckList != null && meetCheckList.size() > 0){
		resultMap.put("returnCode", 1);
		resultMap.put("ListData",meetCheckList);
		resultMap.put("returnMsg", "操作成功");
	}else{
		resultMap.put("returnCode", 0);
		resultMap.put("returnMsg", "系统无数据");
	}
	return resultMap;
}


@Override
public Map<String, Object> selectMeetCheckDetailByPage(
		Map<String, Object> paramsMap) throws DataAccessException {
	Map<String,Object> resultMap = new HashMap<>();
	int pagenum = Integer.parseInt(paramsMap.get("pagenum").toString());
	int pagesize = Integer.parseInt(paramsMap.get("pagesize").toString());
	paramsMap.put("pagestart", (pagenum-1) * pagesize);
	//paramsMap.put("sorting", Optional.ofNullable(paramsMap.get("sorting")).toString().split(","));
	if(paramsMap.get("sorting") != null && paramsMap.get("sorting") != ""){
		paramsMap.put("sorting", paramsMap.get("sorting").toString().split(","));
	}
	List<Map<String,Object>> meetCheckDetailList = meetCheckDao.selectMeetCheckDetailByPage(paramsMap);
	int meetDetsilCheckCounts = meetCheckDao.selectMeetCheckDetailCount(paramsMap);
	if(meetCheckDetailList != null && meetCheckDetailList.size() > 0){
		resultMap.put("returnCode", 1);
		resultMap.put("ListData",meetCheckDetailList);
		resultMap.put("TotalCount",meetDetsilCheckCounts);
		resultMap.put("TotalPage",meetDetsilCheckCounts % pagesize >0 ? meetDetsilCheckCounts/pagesize + 1 : meetDetsilCheckCounts/pagesize);
		resultMap.put("returnMsg", "操作成功");
	}else{
		resultMap.put("returnCode", 0);
		resultMap.put("returnMsg", "系统无数据");
	}
	return resultMap;
}


@Override
public Map<String, Object> updateMeetCheckDetail(Map<String, Object> paramsMap)
		throws DataAccessException {
	Map<String,Object> resultMap = new HashMap<>();
	int k = meetCheckDao.updateMeetCheckDetail(paramsMap);
	if(k <= 0){
		resultMap.put("returnCode", 0);
		resultMap.put("returnMsg", "更新失败");
	}else{
		resultMap.put("returnCode", 1);
		resultMap.put("returnMsg", "更新成功");
	}
	return resultMap;
}

//add yangbo 20180816 例会缺次记录
@Override
public Map<String, Object> findMeetCheckCountService(Map<String, Object> paramsMap) throws DataAccessException {
	Map<String,Object> resultMap = new HashMap<>();
	int pagenum = Integer.parseInt(paramsMap.get("pagenum").toString());
	int pagesize = Integer.parseInt(paramsMap.get("pagesize").toString());
	paramsMap.put("pagestart", (pagenum-1) * pagesize);
	String companyid = Optional.ofNullable(paramsMap.get("companyid")).orElse("").toString();
	paramsMap.put("companyid", companyid.equals("")?"":companyid.split(","));
	List<Map<String,Object>> meetCheckList = meetCheckDao.findMeetCheckCountDao(paramsMap);
	int meetCheckCounts = meetCheckDao.findMeetCheckCountDaoCount(paramsMap);
	if(meetCheckList != null && meetCheckList.size() > 0){
		resultMap.put("returnCode", 1);
		resultMap.put("ListData",meetCheckList);
		resultMap.put("TotalCount",meetCheckCounts);
		resultMap.put("TotalPage",meetCheckCounts % pagesize >0 ? meetCheckCounts/pagesize + 1 : meetCheckCounts/pagesize);
		resultMap.put("returnMsg", "操作成功");
	}else{
		resultMap.put("returnCode", 0);
		resultMap.put("returnMsg", "系统无数据");
	}
	return resultMap;
}


@Override
public Map<String, Object> addBatchMeetCheck(List<Map<String,Object>> dataList)
		throws DataAccessException {
	Map<String,Object> resultMap = new HashMap<>();
	for(int k = 0; k < dataList.size();k ++){
		int i = meetCheckDao.addMeetCheck(dataList.get(k));
		if(i > 0){
			int j = meetCheckDao.addBatchMeetCheckDetail(dataList.get(k));
			if( j >= 0){
				resultMap.put("returnCode", 1);
				resultMap.put("returnMsg", "新增成功");
			}else{
				resultMap.put("returnCode", 0);
				resultMap.put("returnMsg", "新增失败");
			}
		}else{
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "新增失败");
		}
	}
	return resultMap;
}


@Override
public Map<String, Object> updateBatchMeetCheckDetail(
		List<Map<String, Object>> dataList) throws DataAccessException {
	Map<String,Object> resultMap = new HashMap<>();
	int k = meetCheckDao.updateBatchMeetCheckDetail(dataList);
	if(k < 0){
		resultMap.put("returnCode", 0);
		resultMap.put("returnMsg", "更新失败");
	}else{
		resultMap.put("returnCode", 1);
		resultMap.put("returnMsg", "更新成功");
	}
	return resultMap;
}

}
